<template>
  <section class="courses-section">
    <div class="section-header">
      <h3>我的笔记</h3>
      <div class="view-controls">
        <button 
          class="view-btn"
          :class="{ active: viewMode === 'grid' }"
          @click="$emit('view-change', 'grid')"
        >
          <i class="fas fa-th-large"></i>
        </button>
        <button 
          class="view-btn"
          :class="{ active: viewMode === 'list' }"
          @click="$emit('view-change', 'list')"
        >
          <i class="fas fa-list"></i>
        </button>
      </div>
    </div>

    <!-- Empty State -->
    <div class="empty-state" v-if="courses.length === 0">
      <div class="empty-content">
        <i class="fas fa-book-open"></i>
        <h3>还没有笔记</h3>
        <p>开始您的学习之旅</p>
      </div>
    </div>

    <!-- Course Grid -->
    <div class="course-grid" :class="{ 'list-view': viewMode === 'list' }" v-else>
      <div 
        v-for="course in courses" 
        :key="course.id"
        class="course-card"
        @click="viewCourse(course)"
      >
        <div class="course-header">
          <div class="course-title">{{ course.name }}</div>
          <div class="course-actions" v-if="showActions">
            <button 
              class="action-btn" 
              @click.stop="editCourse(course)"
              title="编辑"
            >
              <i class="fas fa-edit"></i>
            </button>
            <button 
              class="action-btn" 
              @click.stop="deleteCourse(course)"
              title="删除"
            >
              <i class="fas fa-trash"></i>
            </button>
          </div>
        </div>
        
        <div class="course-description">
          {{ course.description || '暂无描述' }}
        </div>
        
        <div class="course-meta">
          <div class="meta-item">
            <i class="fas fa-book"></i>
            <span>{{ course.chapters ? course.chapters.length : 0 }} 章节</span>
          </div>
          <div class="meta-item">
            <i class="fas fa-clock"></i>
            <span>{{ formatDate(course.createdAt) }}</span>
          </div>
          <div class="meta-item" v-if="course.lastModified">
            <i class="fas fa-edit"></i>
            <span>{{ formatDate(course.lastModified) }}</span>
          </div>
        </div>
        
        <div class="course-progress" v-if="course.chapters && course.chapters.length > 0">
          <div class="progress-bar">
            <div 
              class="progress-fill" 
              :style="{ width: getProgressPercentage(course) + '%' }"
            ></div>
          </div>
          <div class="progress-text">
            {{ getCompletedChapters(course) }}/{{ course.chapters.length }} 章节完成
          </div>
        </div>
      </div>
    </div>
  </section>
</template>

<script>
export default {
  name: 'CourseSection',
  props: {
    courses: {
      type: Array,
      default: () => []
    },
    currentUser: {
      type: Object,
      default: null
    },
    viewMode: {
      type: String,
      default: 'grid'
    },
    showActions: {
      type: Boolean,
      default: true
    }
  },
  emits: ['view-change', 'show-login', 'show-register'],
  methods: {
    viewCourse(course) {
      this.$router.push({ 
        name: 'CoursePreview', 
        query: { courseId: course.id } 
      })
    },
    editCourse(course) {
      this.$router.push({ 
        name: 'Course', 
        query: { courseId: course.id } 
      })
    },
    async deleteCourse(course) {
      const confirmed = await this.$confirm(
        `确定要删除笔记"${course.name}"吗？此操作不可恢复。`,
        { 
          title: '删除笔记',
          variant: 'error',
          confirmText: '删除'
        }
      )
      
      if (confirmed) {
        const courses = JSON.parse(localStorage.getItem('courses')) || []
        const updatedCourses = courses.filter(c => c.id !== course.id)
        localStorage.setItem('courses', JSON.stringify(updatedCourses))
        
        // 显示删除成功的Toast提示
        this.$toast(`笔记"${course.name}"已删除`, {
          title: '删除成功',
          variant: 'success'
        })
        
        // Emit event to parent to refresh data
        this.$emit('course-deleted', course.id)
        
        // Refresh page data
        window.location.reload()
      }
    },
    formatDate(dateString) {
      if (!dateString) return ''
      const date = new Date(dateString)
      return date.toLocaleDateString('zh-CN', {
        year: 'numeric',
        month: 'short',
        day: 'numeric'
      })
    },
    getCompletedChapters(course) {
      if (!course.chapters) return 0
      return course.chapters.filter(chapter => 
        chapter.content && chapter.content.trim().length > 0
      ).length
    },
    getProgressPercentage(course) {
      if (!course.chapters || course.chapters.length === 0) return 0
      const completed = this.getCompletedChapters(course)
      return Math.round((completed / course.chapters.length) * 100)
    }
  }
}
</script>

<style scoped>
.courses-section {
  margin-bottom: 3rem;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
}

.section-header h3 {
  font-size: 14px;
  font-weight: 600;
  color: #1e293b;
}

.view-controls {
  display: flex;
  gap: 0.5rem;
}

.view-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: 1px solid #e2e8f0;
  background: white;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s ease;
  color: #64748b;
}

.view-btn:hover {
  border-color: #3b82f6;
  color: #3b82f6;
}

.view-btn.active {
  background: #3b82f6;
  border-color: #3b82f6;
  color: white;
}

.empty-state {
  background: white;
  border: 1px solid #e2e8f0;
  border-radius: 12px;
  padding: 3rem 2rem;
  text-align: center;
}

.empty-content i {
  font-size: 48px;
  color: #cbd5e1;
  margin-bottom: 1rem;
}

.empty-content h3 {
  font-size: 14px;
  font-weight: 600;
  color: #1e293b;
  margin-bottom: 0.5rem;
}

.empty-content p {
  font-size: 12px;
  color: #64748b;
  margin-bottom: 1.5rem;
}

.auth-prompt {
  margin-top: 1rem;
}

.auth-prompt p {
  margin-bottom: 1rem;
}

.auth-buttons {
  display: flex;
  gap: 1rem;
  justify-content: center;
}

.btn {
  padding: 0.5rem 1rem;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  text-decoration: none;
  border: 1px solid transparent;
}

.btn-primary {
  background: #3b82f6;
  color: white;
  border-color: #3b82f6;
}

.btn-primary:hover {
  background: #2563eb;
  border-color: #2563eb;
}

.btn-outline {
  background: transparent;
  border-color: #e2e8f0;
  color: #64748b;
}

.btn-outline:hover {
  border-color: #3b82f6;
  color: #3b82f6;
}

.course-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 1rem;
}

.course-grid.list-view {
  grid-template-columns: 1fr;
  gap: 1rem;
}

.course-card {
  background: white;
  border: 1px solid #e2e8f0;
  border-radius: 12px;
  padding: 1.5rem;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.course-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(90deg, #3b82f6, #8b5cf6);
  transform: scaleX(0);
  transform-origin: left;
  transition: transform 0.3s ease;
}

.course-card:hover::before {
  transform: scaleX(1);
}

.course-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
  border-color: #3b82f6;
}

.course-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 1rem;
}

.course-title {
  font-size: 14px;
  font-weight: 600;
  color: #1e293b;
  flex: 1;
  margin-right: 0.5rem;
}

.course-actions {
  display: flex;
  gap: 0.25rem;
  opacity: 0;
  transition: opacity 0.2s ease;
}

.course-card:hover .course-actions {
  opacity: 1;
}

.action-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border: none;
  background: #f1f5f9;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.2s ease;
  color: #64748b;
  font-size: 10px;
}

.action-btn:hover {
  background: #e2e8f0;
  color: #1e293b;
}

.course-description {
  font-size: 12px;
  color: #64748b;
  margin-bottom: 1rem;
  line-height: 1.5;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.course-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  margin-bottom: 1rem;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  font-size: 11px;
  color: #64748b;
}

.meta-item i {
  color: #94a3b8;
}

.course-progress {
  margin-top: 1rem;
}

.progress-bar {
  width: 100%;
  height: 4px;
  background: #e2e8f0;
  border-radius: 2px;
  overflow: hidden;
  margin-bottom: 0.5rem;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #3b82f6, #8b5cf6);
  transition: width 0.3s ease;
}

.progress-text {
  font-size: 10px;
  color: #64748b;
  text-align: center;
}

.list-view .course-card {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  padding: 1rem 1.5rem;
}

.list-view .course-header {
  margin-bottom: 0;
  min-width: 200px;
}

.list-view .course-description {
  margin-bottom: 0;
  flex: 1;
}

.list-view .course-meta {
  margin-bottom: 0;
  gap: 2rem;
}

.list-view .course-progress {
  margin-top: 0;
  min-width: 150px;
}

@media (max-width: 768px) {
  .section-header {
    flex-direction: column;
    gap: 1rem;
    align-items: flex-start;
  }
  
  .view-controls {
    align-self: flex-end;
  }
  
  .course-grid {
    grid-template-columns: 1fr;
  }
  
  .list-view .course-card {
    flex-direction: column;
    align-items: stretch;
    gap: 1rem;
  }
  
  .auth-buttons {
    flex-direction: column;
    align-items: center;
  }
}
</style>